#include "SoldierContainer.h"
#include "Soldier.h"

SoldierContainer::SoldierContainer()
{

}

SoldierContainer::~SoldierContainer()
{
	SOLDIERCONTAINER::iterator itr = m_mapSoldiers.begin();
	while(itr != m_mapSoldiers.end())
	{
		if(itr->second)
		{
			delete itr->second;
			itr->second = 0;
		}

		itr = m_mapSoldiers.erase(itr);
	}
}

void SoldierContainer::AddSoldier( Soldier* pSoldier )
{
	if(!pSoldier)
	{
		return;
	}

	if(CheckSoldier(pSoldier->GetID()))
	{
		return;
	}

	m_mapSoldiers[pSoldier->GetID()] = pSoldier;
}

void SoldierContainer::RemoveSoldier( const Soldier* pSoldier )
{
	if(!pSoldier)
	{
		return;
	}

	SOLDIERCONTAINER::iterator itr = m_mapSoldiers.find(pSoldier->GetID());
	if(itr == m_mapSoldiers.end())
	{
		return;
	}

	m_mapSoldiers.erase(itr);

	delete pSoldier;
	pSoldier = 0;
}

Soldier* SoldierContainer::GetSoldier( unsigned int soldierid )
{
	if(CheckSoldier(soldierid))
	{
		return m_mapSoldiers[soldierid];
	}

	return NULL;
}

SOLDIERCONTAINER SoldierContainer::GetSoldierContainer()
{
	return m_mapSoldiers;
}

bool SoldierContainer::CheckSoldier( unsigned int soldierid )
{
	SOLDIERCONTAINER::iterator itr = m_mapSoldiers.find(soldierid);;
	return itr != m_mapSoldiers.end();
}
